package com.sk.sourcecircle.activity.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.home.activity.EventDetailsActivity;
import com.sk.sourcecircle.adapter.SearchAdapter;
import com.sk.sourcecircle.base.BaseFragment;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.entity.SearchListBean;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.view.EasyLoadingMoreView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by sk on 2017/12/12.
 */

public class SearchNewFragment extends BaseFragment implements HandleDataCallBack, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;

    private List<SearchListBean.DataBean> allDataList = new ArrayList<>();
    private SearchAdapter adapter;
    private boolean isLoadMore = false;

    private int page = 1;
    private String searchString = "";

    @Override
    protected int getContentView() {
        return R.layout.fragment_search;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new SearchAdapter(R.layout.item_search_event,allDataList);
        dataRyc.setAdapter(adapter);
        EasyLoadingMoreView loadMoreView = new EasyLoadingMoreView();
        adapter.setLoadMoreView(loadMoreView);
        View emptyView = getLayoutInflater().inflate(R.layout.empty_view,null);
        adapter.setEmptyView(emptyView);
        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this, dataRyc);
    }

    public void doSearch(String string) {
        searchString = string;
        page = 1;
        allDataList.clear();
        AppApi.searchEvent(null,null,searchString,1,page,this,0,getActivity());
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        SearchListBean searchListBean = JSON.parseObject(result,SearchListBean.class);
        List<SearchListBean.DataBean> dataBeanList = searchListBean.getData();
        allDataList.addAll(dataBeanList);
        if (allDataList.size() < 10) {
            adapter.setEnableLoadMore(false);
        } else {
            adapter.setEnableLoadMore(true);
        }
        if (isLoadMore) {
            adapter.loadMoreComplete();
            if (dataBeanList.size() == 0) {
                adapter.loadMoreEnd();
            }
            isLoadMore = false;
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void handlerFailData(String failString, int tag) {

    }

    @Override
    public void handleErrorData(String error, int tag) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(mContext, EventDetailsActivity.class);
        startActivity(intent);
    }

    @Override
    public void onLoadMoreRequested() {
        isLoadMore = true;
        page++;
        AppApi.searchEvent(null,null,searchString,1,page,this,0,getActivity());
    }
}
